package com.peng.linkedlist;

/**
 * 求链表的中点的相关题目
 */
public class Test01_LinkedListMid {

    public static Node getMidOrUpNode(Node head) {
        Node slow = head;
        Node fast = head;
        while (fast.next != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }

        return slow;
    }


    public static Node getMidOrDownNode(Node head) {
        if (head == null || head.next == null) {
            return head;
        }
        Node slow = head.next;
        Node fast = head.next;
        while (fast.next != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }

        return slow;
    }

    public static Node getMidOrUpPreNode(Node head) {
        if (head == null || head.next == null || head.next.next == null) {
            return null;
        }
        Node slow = head;
        Node fast = head.next.next;
        while (fast.next != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }

        return slow;
    }

    // 1 2 3 4 5 6
    public static Node getMidOrDownPreNode(Node head) {
        if (head == null || head.next == null) {
            return null;
        }
        if (head.next.next == null) {
            return head;
        }

        Node slow = head;
        Node fast = head.next;
        while (fast.next != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }

        return slow;
    }

    /**
     * 奇数个时，获取中点的上一个节点
     * 偶数个时，获取上中点的上一个节点
     *
     * @param head
     * @return
     */
    public static Node getMidOrUpAfterNode(Node head) {
        if (head == null) {
            return null;
        }
        Node slow = head.next;
        Node fast = head;
        while (fast.next != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }

        return slow;
    }

    /**
     * 奇数个时，获取中点的下一个节点
     * 偶数个时，获取下中点的下一个节点
     *
     * @param head
     * @return
     */
    public static Node getMidOrDownAfterNode(Node head) {
        if (head == null || head.next == null) {
            return null;
        }
        Node slow = head.next.next;
        Node fast = head.next;
        while (fast.next != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }

        return slow;
    }

    public static void main(String[] args) {
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        Node node4 = new Node(4);
        Node node5 = new Node(5);
        Node node6 = new Node(6);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;

        System.out.println(getMidOrUpNode(node1).value);
        System.out.println(getMidOrDownNode(node1).value);
        System.out.println(getMidOrUpPreNode(node1).value);
        System.out.println(getMidOrDownPreNode(node1).value);
        System.out.println(getMidOrUpAfterNode(node1).value);
        System.out.println(getMidOrDownAfterNode(node1).value);
    }
}
